package com.qzq.weibo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.entity.User;
import com.qzq.weibo.utils.Utils;

public class UsersAdapter extends ArrayAdapter<User> {

	private LayoutInflater mInflater;
	private int resourceId;

	private ImageView iv_avatar;
	private ImageView iv_gender;
	private TextView tv_screenName;
	private TextView tv_location;

	private TextView tv_statusesNum;
	private TextView tv_followersNum;
	private TextView tv_friendsNum;

	private Utils utils;

	public UsersAdapter(Context context, int resource, List<User> objects) {
		super(context, resource, objects);

		this.mInflater = LayoutInflater.from(context);
		this.resourceId = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = initView(convertView);

		initMember(position);

		return view;
	}

	/** 初始化item布局 */
	private View initView(View convertView) {
		View view = null;
		if (convertView == null) {
			view = mInflater.inflate(resourceId, null);
		} else {
			view = convertView;
		}

		iv_avatar = (ImageView) view.findViewById(R.id.item_user_iv_avatar);
		iv_gender = (ImageView) view.findViewById(R.id.item_user_iv_gender);

		tv_screenName = (TextView) view
				.findViewById(R.id.item_user_tv_screen_name);
		tv_location = (TextView) view.findViewById(R.id.item_user_tv_location);

		tv_statusesNum = (TextView) view
				.findViewById(R.id.item_user_statuses_tv_num);
		tv_followersNum = (TextView) view
				.findViewById(R.id.item_user_followers_tv_num);
		tv_friendsNum = (TextView) view
				.findViewById(R.id.item_user_friends_tv_num);

		return view;
	}

	private void initMember(int position) {

		utils = Utils.getInstance(getContext());

		User user = getItem(position);

		// 动态加载用户头像
		utils.loadBitmap(user.getAvatar_large(), iv_avatar, 40, 40);

		// 设置性别图片
		if (user.getGender().equals("m")) {
			iv_gender.setImageResource(R.drawable.userinfo_male);
		} else {
			iv_gender.setImageResource(R.drawable.userinfo_female);
		}

		// 设置用户其他信息
		tv_screenName.setText(user.getScreen_name());
		tv_location.setText(user.getLocation());

		tv_statusesNum.setText(String.valueOf(user.getStatuses_count()));
		tv_followersNum.setText(String.valueOf(user.getFollowers_count()));
		tv_friendsNum.setText(String.valueOf(user.getFriends_count()));

	}

}
